<?php

namespace App\Http\Controller\AdminPort;

use App\Model\AreaModel;
use Hyperf\Di\Annotation\Inject;
use App\Core\Annotation\NoNeedLogin;
use Lhp\JigsawPuzzleVerify\Verify;


/**
 *  拼图验证码
 */
class Captcha
{
    #[Inject]
    protected Verify $verify;

    /**
     * @return string[]
     */
    #[NoNeedLogin]
    public function get()
    {
        $data = $this->verify->getNewImage();
        return getResponseData('', [
            'background' => $data['background'],
            'concave' => $data['concave'],
            'concaveHeight' => $data['concaveHeight']
        ]);
    }

    /**
     * 验证
     *
     */
    public function postVerify()
    {
        validate([
            ['标识key', 'captchaKey', 'required|string|max:500'],
            ['截图位置', 'captchaX', 'required|number|min:0|max:500']
        ]);

        try {
            if ( ! $this->verify->verify(I('captchaKey'), I('captchaX'))) {
                responseError('验证失败');
            }
        } catch (\Exception $e) {
            responseError('验证过期，请重新验证');
        }

        return getResponseData('验证成功');
    }
}
